<?php 
class SpaceTagsModel extends Model{
	protected $tableName = "space_tag";
	//添加Tag
	function doadd($uid,$tagname){
		if(h($tagname)){
			$tagId = model('Xtag')->addTag($tagname);
			return $this->doaddone($uid,$tagId);
		}else{
			return '-1';
		}
	}
	
	function doaddone($uid,$tagId){
		if(0==$this->table(C('DB_PREFIX')."space_tag")->where('uid='.$uid.' AND tagid='.$tagId)->count() && $tagId){
			$data['uid']   = $uid;
			$data['tagid'] = $tagId;
			//return M("space_tag")->add($data);
			if( $this->add($data) ){
				return $tagId;
			}
		}else{
			return 0;
		}
	}
	
	function doget($uid){
		$li = $this->where('uid='.$uid)->field('tagid')->order('id ASC')->findall();
		foreach ($li as $v){
			if(0==$this->table(C('DB_PREFIX')."x_tag")->field('tagid,tagname')->where('tagid='.$v['tagid'])->count()){
				
			}else{
				$l[] = $this->table(C('DB_PREFIX')."x_tag")->field('tagid,tagname')->where('tagid='.$v['tagid'])->find();
			}
		}
		return $l;
	}
	
	function dodel($uid,$tagid){
		return $this->table(C('DB_PREFIX')."space_tag")->where("uid=$uid AND tagid=$tagid")->delete();
	}
	
	function getCommendList($uid){
		$list = $this->query("SELECT `tagid` as tagid,count(`uid`) as count FROM ".C("DB_PREFIX")."space_tag WHERE tagid NOT IN (SELECT tagid FROM ".C('DB_PREFIX')."space_tag WHERE uid=$uid) GROUP BY `tagid` HAVING count(`uid`) >0 ORDER BY count DESC LIMIT 15");
		foreach ($list as $v){
			$l[] = $v['tagid'];
		}
		return $this->query("SELECT tagname,tagid FROM ".C('DB_PREFIX')."x_tag WHERE tagid IN (".implode(',',$l).")");
	}
}
?>